<template>
	<div class="order-btn-container margin">
		<div class="c-btn big black" @click="popshow = true">退回</div>
		<div class="c-btn big reverse"
		:class="{disabled: uploading || !repairForm.txt}"
		@click="add">提交</div>
		<pop :popshow="popshow"
		:popstyle="{width:'8.5rem'}">
			<txtpop title="退回原因"
			:maxlength="200"
			placeholder="请输入退回原因"
			@cancel="popshow = false"
			@send="back"></txtpop>
		</pop>
	</div>
</template>
<script>
	import {myAlert, myHttp} from '../assets/js/utils.js'
	import {tip} from '../assets/js/tip.js'
	import pop from '../Pop.vue'
	import txtpop from '../TxtPop.vue'
	export default{
		name: 'RepairmanConfirmBtn',
		data(){
			return{
				uploading: false,
				popshow: false,
			}
		},
		methods:{
			back(txt){
				if(this.uploading) return;
				if(!txt) {
					myAlert.small('请输入退回原因');
					return;
				}
				myHttp('backToDept.do',{
					repairId:  this.$route.params.id,
					userId: localStorage.getItem('userId'),
					backReason: txt
				}, () => {
					this.uploading = true
				}).then((r) => {
					if(r.failed) {
						this.uploading = false;
						return;
					}
					myAlert.small('已退回此单').then(() => {
						if(history.length == 1) this.$router.push({path:'/repair/list/repairman'});
						else this.$router.go(-1);
					})
				})
			},
			add(){
				if(this.uploading) return;
				if(!this.repairForm.txt) return;
				let fm = new FormData();
				fm.append('repairId', this.$route.params.id);
				fm.append('userId', localStorage.getItem('userId'));
				this.repairForm.img.forEach((img) => {
					fm.append('imageFiles', img, `${Math.ceil(Date.now()*Math.random()*10)}.jpg`);
				})
				fm.append('mediaId', this.repairForm.audio);
				fm.append('handleDesc', this.repairForm.txt);
				fm.append('licenseCode', 'E2ECBC62529F0D090EA369022622F74B');
				this.repairForm.isFinished ? this.addResult(fm) : this.addProcess(fm);
			},
			addProcess(fm){
				myHttp('addHandleProcess.do', fm, () => {
					this.uploading = true
				}, {tip: true}).then((r) => {
					if(r.failed) {
						this.uploading = false;
						return;
					}
					myAlert.small(tip.ADD_PROCESS).then(() => {
						if(history.length == 1) this.$router.push({path:'/repair/list/repairman'});
						else this.$router.go(-1);
					})
				})
			},
			addResult(fm){
				myHttp('addHandleSource.do', fm, () => {}, {
					tip: true
				}).then((r) => {
					if(r.failed) {
						this.uploading = false;
						return;
					}
					myAlert.small(tip.ADD_RESULT).then(() => {
						if(history.length == 1) this.$router.push({path:'/repair/list/repairman'});
						else this.$router.go(-1);
					})
				})
			}
		},
		props:['repairForm'],
		components: {pop, txtpop}
	}
</script>